package com.jdd.modules.system.controller;

import com.jdd.common.api.vo.Result;
import com.jdd.modules.system.entity.SysDataSyncConfigDTO;
import com.jdd.modules.system.service.ISysDataSyncConfigService;
import com.jdd.modules.system.service.ISysDataSyncRecordService;
import com.jdd.modules.system.util.SyncUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
@RequestMapping("/sys/dataSync")
@Controller
public class SysDataSyncController {

    @Resource
    ISysDataSyncRecordService syncRecordService;
    @Resource
    ISysDataSyncConfigService syncConfigService;
    @Resource
    SyncUtil syncUtil;

    /**
     * 注意多线程同步问题
     */
    ReentrantLock syncLock= new ReentrantLock();

    @GetMapping("/newestSyncInfo")
    @ResponseBody
    @ApiOperation(value = "获取最新的数据同步列表信息")
    public Result<List<SysDataSyncConfigDTO>> listNewestSyncInfo(){

        List<SysDataSyncConfigDTO> sysDataSyncConfigDTOS = syncConfigService.listAllConfigWithRecord();
        if(sysDataSyncConfigDTOS.isEmpty()){
            return Result.error("查询为空, 请检查是否初始化表sys_data_sync_config");
        }
        return Result.ok(sysDataSyncConfigDTOS);
    }


    /**
     * 同步单项云端数据
     * @param body ： 同步大项
     * @return
     */
    @RequestMapping(value = "/syncDataItem",method = {RequestMethod.POST})
    @ResponseBody
    @ApiOperation(value = "根据数据类型同步云端数据")
    public Result syncDataItem(@RequestBody Map<String,Object> body){
        String dataName = (String)body.get("dataName");
        boolean locked=false;
            try {
                 locked = syncLock.tryLock();
                if(!locked){
                    return Result.error("正在同步，请勿重复请求");
                }
                log.info(dataName);
                syncUtil.syncByConfigName(dataName);
                return Result.ok("success");
            }catch (Exception e){
                log.error("同步失败,",e);
                return Result.error("同步失败:"+e.getMessage());
            }finally {
                if(locked){
                    syncLock.unlock();
                }
            }
    }

    /**
     * 同步所有数据
     * @return
     */
    @RequestMapping(value = "/syncAllData",method = {RequestMethod.POST})
    @ResponseBody
    @ApiOperation(value = "同步所有云端数据")
    public Result syncAllData(){
        boolean locked=false;
        try {
            locked = syncLock.tryLock();
            if(!locked){
                return Result.error("正在同步，请勿重复请求");
            }
            syncUtil.syncAllConfig();
            return Result.ok("success");
        }catch (Exception e){
            log.error("同步失败,",e);
            return Result.error("同步失败:"+e.getMessage());
        }finally {
            if(locked){
                syncLock.unlock();
            }
        }
    }


}




